package stendcontrol;

public class CommandMessage extends AbstractMessage {

    public final static int MOTION = 1;
    public final static int SET_CUR_ENGINE = 2;
    public final static int SET_SPEED = 3;
    public final static int GO_HOME = 4;
    public final static int EMERG_STOP = 5;
    public final static int AKNWNES = 6;
    public final static int TARGET_SENDER = 7;
    public final static int RESEARCH_TYPE = 8;
    public final static int SET_SMPL_INTRV = 9;
    public final static int SELECT_SENSOR_TYPE = 10;
    public final static int GET_ENGINE_POS = 11;
    private final static int msgCnt = 11;
    private final boolean islongPacket[] = {
        false, //0-Пустота
        false, //1-Шаги
        false, //2-Выбор двигателя
        false, //3-Выбор скорости
        false, //4-В исходное положение
        false, //5-Экстренная остановка
        false, //6-Квитирование 
        true, //7-Целевое положение
        false, //8-Тип исследования
        false, //9-Установка интервала дискретизации
        false, //10 - Выбор чувствительного датчика
        false //11 - Запрос позиции двигателя
    };
    private int msgtype;
    private int id;
    private byte shrtPack;
    private byte longPack[] = new byte[4];


    /**
     *
     */
    public CommandMessage(int msgType, byte pr, long prm) {
        if (msgType >= 0 && msgType <= CommandMessage.msgCnt) {
            this.msgtype = msgType;
        } else {
            StendLogger.log(StendLogger.MSG_ERROR, "Bad index in CommandMessage"
                    + " msgType:" + msgType);
        }
       // System.out.println("srt pack:"+pr);
        this.shrtPack = pr;
        this.longPack[0] = (byte) (prm >> 24);
        this.longPack[1] = (byte) ((prm >> 16) & 0xff);
        this.longPack[2] = (byte) ((prm >> 8) & 0xff);
        this.longPack[3] = (byte) ((prm) & 0xff);
    }

    /**
     * Operation Установка параметра
     *
     * @param параметр
     * @return
     */
    public byte[] getBytes() {
        byte rbuf[];
        /*System.out.println("ID -> " + this.id + "\n" + " msg type:  " + this.
                msgtype);*/
        if (islongPacket[msgtype]) {       
            rbuf = new byte[6];
            rbuf[0] = (byte) (((msgtype & 0xf) << 4) | (id & 0xf));
            rbuf[1] = shrtPack;
            System.arraycopy(longPack, 0, rbuf, 2, longPack.length);
            return rbuf;
        } else {
            rbuf = new byte[2];
            rbuf[0] = (byte) (((msgtype & 0xf) << 4) | (id & 0xf));
            rbuf[1] = shrtPack;
            return rbuf;
        }
    }

    /**
     * Большой пакет
     *
     * @param prm
     */
    public void setLongParam(long prm) {
        longPack = new byte[4];
        //longPack[1] = 0;
        longPack[0] = (byte) (prm >> 24);
        longPack[1] = (byte) ((prm >> 16) & 0xff);
        longPack[2] = (byte) ((prm >> 8) & 0xff);
        longPack[3] = (byte) ((prm) & 0xff);
        //   longPacket = true;
    }

    /**
     * Маленький пакет
     *
     * @param prm
     */
    public void setShortParam(byte prm) {
        shrtPack = prm;
    }

    /**
     * Operation Выбор типа сообщения
     *
     * @param type
     * @return
     */
    public void setType(int type) {
        msgtype = type;
    }

    @Override
    public int getTypeMsg() {
        return AbstractMessage.MSG_CMD;
    }

    public void setId(int id) {
        if (id >= 0 && id < 16) {
            this.id = id;
        } else {
            StendLogger.log(StendLogger.MSG_ERROR, "Bad id for CommandMessge:"
                    + id);
        }
    }
}
